package logic

import (
	"svvenus/conf"
	"svvenus/util/uttype"
	"svvenus/vpub/serve"
	"svvenus/zlog"
	"svvenus/znet/zpb"
)

// ItemValue
// @Description: 道具数目
// @receiver player
// @param cfgId
// @return int64
func (player *Player) ItemValue(cfgId int) int64 {
	v, ok := player.Items[uttype.IntToString(cfgId)]
	if ok {
		return v
	} else {
		return 0
	}
}

// ItemHas
// @Description: 道具判断
// @receiver player
// @param cfgId
// @param value
// @return bool
func (player *Player) ItemHas(cfgId int, value int64) bool {
	return player.ItemValue(cfgId) >= value
}

// ItemAdd
// @Description: 道具新增
// @receiver player
// @param cfgId
// @param value
// @param sor
func (player *Player) ItemAdd(cfgId int, value int64, sor zpb.RecSource) {
	if value < 0 {
		return
	}
	if _, ok := conf.MapItem[int(cfgId)]; !ok {
		zlog.Warn("ItemAdd error Id:", cfgId)
		return
	}
	player.Items[uttype.IntToString(cfgId)] += value

	serve.RecordAdd(zpb.RecType_rtPlayerItemAdd, sor, player.IdString(), player.Name, uttype.IntToString(cfgId), uttype.Int64ToString(value))
}

// ItemDel
// @Description: 道具删除
// @receiver player
// @param cfgId
// @param value
// @param sor
func (player *Player) ItemDel(cfgId int, value int64, sor zpb.RecSource) {
	if value < 0 {
		return
	}
	player.Items[uttype.IntToString(cfgId)] -= value

	serve.RecordAdd(zpb.RecType_rtPlayerItemDel, sor, player.IdString(), player.Name, uttype.IntToString(cfgId), uttype.Int64ToString(value))
}

func (player *Player) ItemInfoList() *zpb.ItemInfoList {
	info := &zpb.ItemInfoList{}
	info.Items = make(map[int32]int64)
	for k, v := range player.Items {
		info.Items[uttype.StringToInt32(k)] = v
	}
	return info
}

func (player *Player) PushItemInfoList() {
	rsp := player.ItemInfoList()
	player.Send(zpb.EPid_GCItemInfoPush, 0, rsp)
}
